<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>tableeditor_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_plugins_tableeditor_test.js.html">tableeditor_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.editor.plugins.TableEditorTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.editor.plugins.TableEditorTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.Range&#39;);
<a name="line20"></a>goog.require(&#39;goog.editor.plugins.TableEditor&#39;);
<a name="line21"></a>goog.require(&#39;goog.object&#39;);
<a name="line22"></a>goog.require(&#39;goog.string&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.ExpectedFailures&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.JsUnitException&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.editor.FieldMock&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.editor.TestHelper&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line29"></a>
<a name="line30"></a>var field;
<a name="line31"></a>var plugin;
<a name="line32"></a>var fieldMock;
<a name="line33"></a>var expectedFailures;
<a name="line34"></a>var testHelper;
<a name="line35"></a>
<a name="line36"></a>function setUpPage() {
<a name="line37"></a>  field = goog.dom.getElement(&#39;field&#39;);
<a name="line38"></a>  expectedFailures = new goog.testing.ExpectedFailures();
<a name="line39"></a>}
<a name="line40"></a>
<a name="line41"></a>function setUp() {
<a name="line42"></a>  testHelper = new goog.testing.editor.TestHelper(
<a name="line43"></a>      goog.dom.getElement(&#39;field&#39;));
<a name="line44"></a>  testHelper.setUpEditableElement();
<a name="line45"></a>  field.focus();
<a name="line46"></a>  plugin = new goog.editor.plugins.TableEditor();
<a name="line47"></a>  fieldMock = new goog.testing.editor.FieldMock();
<a name="line48"></a>  plugin.registerFieldObject(fieldMock);
<a name="line49"></a>  if (goog.userAgent.IE &amp;&amp;
<a name="line50"></a>      (goog.userAgent.compare(goog.userAgent.VERSION, &#39;7.0&#39;) &gt;= 0)) {
<a name="line51"></a>    goog.testing.TestCase.protectedTimeout_ = window.setTimeout;
<a name="line52"></a>  }
<a name="line53"></a>}
<a name="line54"></a>
<a name="line55"></a>function tearDown() {
<a name="line56"></a>  testHelper.tearDownEditableElement();
<a name="line57"></a>  expectedFailures.handleTearDown();
<a name="line58"></a>}
<a name="line59"></a>
<a name="line60"></a>function testEnable() {
<a name="line61"></a>  fieldMock.$replay();
<a name="line62"></a>
<a name="line63"></a>  plugin.enable(fieldMock);
<a name="line64"></a>  assertTrue(&#39;Plugin should be enabled&#39;, plugin.isEnabled(fieldMock));
<a name="line65"></a>
<a name="line66"></a>  if (goog.userAgent.GECKO) {
<a name="line67"></a>    // This code path is executed only for GECKO browsers but we can&#39;t
<a name="line68"></a>    // verify it because of a GECKO bug while reading the value of the
<a name="line69"></a>    // command &quot;enableObjectResizing&quot;.
<a name="line70"></a>    // See https://bugzilla.mozilla.org/show_bug.cgi?id=506368
<a name="line71"></a>    expectedFailures.expectFailureFor(goog.userAgent.GECKO);
<a name="line72"></a>    try {
<a name="line73"></a>      var doc = plugin.getFieldDomHelper().getDocument();
<a name="line74"></a>      assertTrue(&#39;Object resizing should be enabled&#39;,
<a name="line75"></a>                 doc.queryCommandValue(&#39;enableObjectResizing&#39;));
<a name="line76"></a>    } catch (e) {
<a name="line77"></a>      // We need to marshal our exception in order for it to be handled
<a name="line78"></a>      // properly.
<a name="line79"></a>      expectedFailures.handleException(new goog.testing.JsUnitException(e));
<a name="line80"></a>    }
<a name="line81"></a>  }
<a name="line82"></a>  fieldMock.$verify();
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>function testIsSupportedCommand() {
<a name="line86"></a>  goog.object.forEach(goog.editor.plugins.TableEditor.COMMAND,
<a name="line87"></a>      function(command) {
<a name="line88"></a>        assertTrue(goog.string.subs(&#39;Plugin should support %s&#39;, command),
<a name="line89"></a>            plugin.isSupportedCommand(command));
<a name="line90"></a>      });
<a name="line91"></a>  assertFalse(&#39;Plugin shouldn\&#39;t support a bogus command&#39;,
<a name="line92"></a>              plugin.isSupportedCommand(&#39;+fable&#39;));
<a name="line93"></a>}
<a name="line94"></a>
<a name="line95"></a>function testCreateTable() {
<a name="line96"></a>  fieldMock.$replay();
<a name="line97"></a>  createTableAndSelectCell();
<a name="line98"></a>  var table = plugin.getCurrentTable_();
<a name="line99"></a>  assertNotNull(&#39;Table should not be null&#39;, table);
<a name="line100"></a>  assertEquals(&#39;Table should have the default number of rows&#39;,
<a name="line101"></a>               2,
<a name="line102"></a>               table.rows.length);
<a name="line103"></a>  assertEquals(&#39;Table should have the default number of cells&#39;,
<a name="line104"></a>               8,
<a name="line105"></a>               getCellCount(table));
<a name="line106"></a>  fieldMock.$verify();
<a name="line107"></a>}
<a name="line108"></a>
<a name="line109"></a>function testInsertRowBefore() {
<a name="line110"></a>  fieldMock.$replay();
<a name="line111"></a>  createTableAndSelectCell();
<a name="line112"></a>  var table = plugin.getCurrentTable_();
<a name="line113"></a>  var selectedRow = fieldMock.getRange().getContainerElement().parentNode;
<a name="line114"></a>  assertNull(&#39;Selected row shouldn\&#39;t have a previous sibling&#39;,
<a name="line115"></a>             selectedRow.previousSibling);
<a name="line116"></a>  assertEquals(&#39;Table should have two rows&#39;, 2, table.rows.length);
<a name="line117"></a>  plugin.execCommandInternal(
<a name="line118"></a>      goog.editor.plugins.TableEditor.COMMAND.INSERT_ROW_BEFORE);
<a name="line119"></a>  assertEquals(&#39;A row should have been inserted&#39;, 3, table.rows.length);
<a name="line120"></a>
<a name="line121"></a>  // Assert that we inserted a row above the currently selected row.
<a name="line122"></a>  assertNotNull(&#39;Selected row should have a previous sibling&#39;,
<a name="line123"></a>                selectedRow.previousSibling);
<a name="line124"></a>  fieldMock.$verify();
<a name="line125"></a>}
<a name="line126"></a>
<a name="line127"></a>function testInsertRowAfter() {
<a name="line128"></a>  fieldMock.$replay();
<a name="line129"></a>  createTableAndSelectCell({width: 2, height: 1});
<a name="line130"></a>  var selectedRow = fieldMock.getRange().getContainerElement().parentNode;
<a name="line131"></a>  var table = plugin.getCurrentTable_();
<a name="line132"></a>  assertEquals(&#39;Table should have one row&#39;, 1, table.rows.length);
<a name="line133"></a>  assertNull(&#39;Selected row shouldn\&#39;t have a next sibling&#39;,
<a name="line134"></a>             selectedRow.nextSibling);
<a name="line135"></a>  plugin.execCommandInternal(
<a name="line136"></a>      goog.editor.plugins.TableEditor.COMMAND.INSERT_ROW_AFTER);
<a name="line137"></a>  assertEquals(&#39;A row should have been inserted&#39;, 2, table.rows.length);
<a name="line138"></a>  // Assert that we inserted a row after the currently selected row.
<a name="line139"></a>  assertNotNull(&#39;Selected row should have a next sibling&#39;,
<a name="line140"></a>                selectedRow.nextSibling);
<a name="line141"></a>  fieldMock.$verify();
<a name="line142"></a>}
<a name="line143"></a>
<a name="line144"></a>function testInsertColumnBefore() {
<a name="line145"></a>  fieldMock.$replay();
<a name="line146"></a>  createTableAndSelectCell({width: 1, height: 1});
<a name="line147"></a>  var table = plugin.getCurrentTable_();
<a name="line148"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line149"></a>  assertEquals(&#39;Table should have one cell&#39;, 1, getCellCount(table));
<a name="line150"></a>  assertNull(&#39;Selected cell shouldn\&#39;t have a previous sibling&#39;,
<a name="line151"></a>             selectedCell.previousSibling);
<a name="line152"></a>  plugin.execCommandInternal(
<a name="line153"></a>      goog.editor.plugins.TableEditor.COMMAND.INSERT_COLUMN_BEFORE);
<a name="line154"></a>  assertEquals(&#39;A cell should have been inserted&#39;, 2, getCellCount(table));
<a name="line155"></a>  assertNotNull(&#39;Selected cell should have a previous sibling&#39;,
<a name="line156"></a>                selectedCell.previousSibling);
<a name="line157"></a>  fieldMock.$verify();
<a name="line158"></a>}
<a name="line159"></a>
<a name="line160"></a>function testInsertColumnAfter() {
<a name="line161"></a>  fieldMock.$replay();
<a name="line162"></a>  createTableAndSelectCell({width: 1, height: 1});
<a name="line163"></a>  var table = plugin.getCurrentTable_();
<a name="line164"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line165"></a>  assertEquals(&#39;Table should have one cell&#39;, 1, getCellCount(table));
<a name="line166"></a>  assertNull(&#39;Selected cell shouldn\&#39;t have a next sibling&#39;,
<a name="line167"></a>             selectedCell.nextSibling);
<a name="line168"></a>  plugin.execCommandInternal(
<a name="line169"></a>      goog.editor.plugins.TableEditor.COMMAND.INSERT_COLUMN_AFTER);
<a name="line170"></a>  assertEquals(&#39;A cell should have been inserted&#39;, 2, getCellCount(table));
<a name="line171"></a>  assertNotNull(&#39;Selected cell should have a next sibling&#39;,
<a name="line172"></a>                selectedCell.nextSibling);
<a name="line173"></a>  fieldMock.$verify();
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>function testRemoveRows() {
<a name="line177"></a>  fieldMock.$replay();
<a name="line178"></a>  createTableAndSelectCell({width: 1, height: 2});
<a name="line179"></a>  var table = plugin.getCurrentTable_();
<a name="line180"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line181"></a>  selectedCell.id = &#39;selected&#39;;
<a name="line182"></a>  assertEquals(&#39;Table should have two rows&#39;, 2, table.rows.length);
<a name="line183"></a>  plugin.execCommandInternal(
<a name="line184"></a>      goog.editor.plugins.TableEditor.COMMAND.REMOVE_ROWS);
<a name="line185"></a>  assertEquals(&#39;A row should have been removed&#39;, 1, table.rows.length);
<a name="line186"></a>  assertNull(&#39;The correct row should have been removed&#39;,
<a name="line187"></a>             goog.dom.getElement(&#39;selected&#39;));
<a name="line188"></a>
<a name="line189"></a>  // Verify that the table is removed if we don&#39;t have any rows.
<a name="line190"></a>  plugin.execCommandInternal(
<a name="line191"></a>      goog.editor.plugins.TableEditor.COMMAND.REMOVE_ROWS);
<a name="line192"></a>  assertEquals(&#39;The table should have been removed&#39;,
<a name="line193"></a>               0,
<a name="line194"></a>               field.getElementsByTagName(&#39;table&#39;).length);
<a name="line195"></a>  fieldMock.$verify();
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>function testRemoveColumns() {
<a name="line199"></a>  fieldMock.$replay();
<a name="line200"></a>  createTableAndSelectCell({width: 2, height: 1});
<a name="line201"></a>  var table = plugin.getCurrentTable_();
<a name="line202"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line203"></a>  selectedCell.id = &#39;selected&#39;;
<a name="line204"></a>  assertEquals(&#39;Table should have two cells&#39;, 2, getCellCount(table));
<a name="line205"></a>  plugin.execCommandInternal(
<a name="line206"></a>      goog.editor.plugins.TableEditor.COMMAND.REMOVE_COLUMNS);
<a name="line207"></a>  assertEquals(&#39;A cell should have been removed&#39;, 1, getCellCount(table));
<a name="line208"></a>  assertNull(&#39;The correct cell should have been removed&#39;,
<a name="line209"></a>             goog.dom.getElement(&#39;selected&#39;));
<a name="line210"></a>
<a name="line211"></a>  // Verify that the table is removed if we don&#39;t have any columns.
<a name="line212"></a>  plugin.execCommandInternal(
<a name="line213"></a>      goog.editor.plugins.TableEditor.COMMAND.REMOVE_COLUMNS);
<a name="line214"></a>  assertEquals(&#39;The table should have been removed&#39;,
<a name="line215"></a>               0,
<a name="line216"></a>               field.getElementsByTagName(&#39;table&#39;).length);
<a name="line217"></a>  fieldMock.$verify();
<a name="line218"></a>}
<a name="line219"></a>
<a name="line220"></a>function testSplitCell() {
<a name="line221"></a>  fieldMock.$replay();
<a name="line222"></a>  createTableAndSelectCell({width: 1, height: 1});
<a name="line223"></a>  var table = plugin.getCurrentTable_();
<a name="line224"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line225"></a>  // Splitting is only supported if we set these attributes.
<a name="line226"></a>  selectedCell.rowSpan = &#39;1&#39;;
<a name="line227"></a>  selectedCell.colSpan = &#39;2&#39;;
<a name="line228"></a>  selectedCell.innerHTML = &#39;foo&#39;;
<a name="line229"></a>  goog.dom.Range.createFromNodeContents(selectedCell).select();
<a name="line230"></a>  assertEquals(&#39;Table should have one cell&#39;, 1, getCellCount(table));
<a name="line231"></a>  plugin.execCommandInternal(
<a name="line232"></a>      goog.editor.plugins.TableEditor.COMMAND.SPLIT_CELL);
<a name="line233"></a>  assertEquals(&#39;The cell should have been split&#39;, 2, getCellCount(table));
<a name="line234"></a>  assertEquals(&#39;The cell content should be intact&#39;,
<a name="line235"></a>               &#39;foo&#39;,
<a name="line236"></a>               selectedCell.innerHTML);
<a name="line237"></a>  assertNotNull(&#39;The new cell should be inserted before&#39;,
<a name="line238"></a>      selectedCell.previousSibling);
<a name="line239"></a>  fieldMock.$verify();
<a name="line240"></a>}
<a name="line241"></a>
<a name="line242"></a>function testMergeCells() {
<a name="line243"></a>  fieldMock.$replay();
<a name="line244"></a>  createTableAndSelectCell({width: 2, height: 1});
<a name="line245"></a>  var table = plugin.getCurrentTable_();
<a name="line246"></a>  var selectedCell = fieldMock.getRange().getContainerElement();
<a name="line247"></a>  selectedCell.innerHTML = &#39;foo&#39;;
<a name="line248"></a>  selectedCell.nextSibling.innerHTML = &#39;bar&#39;;
<a name="line249"></a>  var range = goog.dom.Range.createFromNodeContents(
<a name="line250"></a>      table.getElementsByTagName(&#39;tr&#39;)[0]);
<a name="line251"></a>  range.select();
<a name="line252"></a>  plugin.execCommandInternal(
<a name="line253"></a>      goog.editor.plugins.TableEditor.COMMAND.MERGE_CELLS);
<a name="line254"></a>  expectedFailures.expectFailureFor(
<a name="line255"></a>      goog.userAgent.IE &amp;&amp;
<a name="line256"></a>      goog.userAgent.isVersionOrHigher(&#39;8&#39;));
<a name="line257"></a>  try {
<a name="line258"></a>    // In IE8, even after explicitly setting the range to span
<a name="line259"></a>    // multiple cells, the browser selection only contains the first TD
<a name="line260"></a>    // which causes the merge operation to fail.
<a name="line261"></a>    assertEquals(&#39;The cells should be merged&#39;, 1, getCellCount(table));
<a name="line262"></a>    assertEquals(&#39;The cell should have expected colspan&#39;,
<a name="line263"></a>                 2,
<a name="line264"></a>                 selectedCell.colSpan);
<a name="line265"></a>    assertHTMLEquals(&#39;The content should be merged&#39;,
<a name="line266"></a>                     &#39;foo bar&#39;,
<a name="line267"></a>                     selectedCell.innerHTML);
<a name="line268"></a>  } catch (e) {
<a name="line269"></a>    expectedFailures.handleException(e);
<a name="line270"></a>  }
<a name="line271"></a>  fieldMock.$verify();
<a name="line272"></a>}
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Helper routine which returns the number of cells in the table.
<a name="line277"></a> *
<a name="line278"></a> * @param {Element} table The table in question.
<a name="line279"></a> * @return {number} Number of cells.
<a name="line280"></a> */
<a name="line281"></a>function getCellCount(table) {
<a name="line282"></a>  return table.cells ? table.cells.length :
<a name="line283"></a>      table.rows[0].cells.length * table.rows.length;
<a name="line284"></a>}
<a name="line285"></a>
<a name="line286"></a>
<a name="line287"></a>/**
<a name="line288"></a> * Helper method which creates a table and puts the cursor on the first TD.
<a name="line289"></a> * In IE, the cursor isn&#39;t positioned in the first cell (TD) and we simulate
<a name="line290"></a> * that behavior explicitly to be consistent across all browsers.
<a name="line291"></a> *
<a name="line292"></a> * @param {Object} op_tableProps Optional table properties.
<a name="line293"></a> */
<a name="line294"></a>function createTableAndSelectCell(opt_tableProps) {
<a name="line295"></a>  goog.dom.Range.createCaret(field, 1).select();
<a name="line296"></a>  plugin.execCommandInternal(goog.editor.plugins.TableEditor.COMMAND.TABLE,
<a name="line297"></a>                             opt_tableProps);
<a name="line298"></a>  if (goog.userAgent.IE) {
<a name="line299"></a>    var range = goog.dom.Range.createFromNodeContents(
<a name="line300"></a>        field.getElementsByTagName(&#39;td&#39;)[0]);
<a name="line301"></a>    range.select();
<a name="line302"></a>  }
<a name="line303"></a>}
</pre>


</body>
</html>
